/**
 * @Date 2020-04-17 18:10:46
 * @Remark
 */

// lib
import { Toast } from "vant";
// config
// script & methods & public
// biz
// store
// service
// interface && type
// 其它

function msg(text: string) {
  Toast(text);
}

function ok(text: string) {
  Toast.success(text);
}

function warn(text: string) {
  Toast.fail(text);
}

function error(text: string) {
  Toast.fail(text);
}

function loading(text = "加载中") {
  Toast.loading({
    message: text,
    duration: 0,
  });
}

function hide() {
  Toast.clear(true);
}

// 失败或错误时提示
function whenFailOrErr<T>(result: BagData<T> | null) {
  if (!result) {
    return;
  }
  if (!result.payload) {
    if (result.fail.length > 0) {
      hide();
      setTimeout(() => {
        warn(result.fail[0].message);
      }, 100);
      return;
    }
    if (result.err.length > 0) {
      hide();
      setTimeout(() => {
        error(result.err[0].message);
      }, 100);
    }
  }
}

// 验证失败时提示
function validFail(result: any) {
  if (result.length > 0) {
    warn(result[0].message);
    return false;
  }
  return true;
}

export default {
  msg,
  ok,
  warn,
  error,
  loading,
  hide,
  validFail,
  whenFailOrErr,
};
